package org.example.leetCode;

import java.util.Arrays;

/**
 * 189. 轮转数组
 * 给定一个整数数组 nums，将数组中的元素向右轮转 k 个位置，其中 k 是非负数。
 */
public class Code189 {
    public void rotate(int[] nums, int k) {
        int n=nums.length;
        k%=n;
        rever(nums,0,n-1);
        rever(nums,0,k-1);
        rever(nums,k,n-1);
    }

    private void rever(int[] nums, int start, int end) {
        int temp;
        while (start<end){
            temp=nums[start];
            nums[start]=nums[end];
            nums[end]=temp;
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        Code189 solution = new Code189();

        // 测试用例1: nums = [1,2,3,4,5,6,7], k = 3
        int[] nums1 = {1, 2, 3, 4, 5, 6, 7};
        System.out.println("原始数组: " + Arrays.toString(nums1));
        solution.rotate(nums1, 3);
        System.out.println("向右轮转3个位置后: " + Arrays.toString(nums1));
        // 预期输出: [5,6,7,1,2,3,4]

        // 测试用例2: nums = [-1,-100,3,99], k = 2
        int[] nums2 = {-1, -100, 3, 99};
        System.out.println("原始数组: " + Arrays.toString(nums2));
        solution.rotate(nums2, 2);
        System.out.println("向右轮转2个位置后: " + Arrays.toString(nums2));
        // 预期输出: [3,99,-1,-100]

        // 测试用例3: nums = [1,2], k = 1
        int[] nums3 = {1, 2};
        System.out.println("原始数组: " + Arrays.toString(nums3));
        solution.rotate(nums3, 1);
        System.out.println("向右轮转1个位置后: " + Arrays.toString(nums3));
        // 预期输出: [2,1]

        // 测试用例4: nums = [1,2,3], k = 6 (k > 数组长度)
        int[] nums4 = {1, 2, 3};
        System.out.println("原始数组: " + Arrays.toString(nums4));
        solution.rotate(nums4, 6);
        System.out.println("向右轮转6个位置后: " + Arrays.toString(nums4));
        // 预期输出: [1,2,3] (6 % 3 = 0，数组不变)

        // 测试用例5: nums = [1], k = 0
        int[] nums5 = {1};
        System.out.println("原始数组: " + Arrays.toString(nums5));
        solution.rotate(nums5, 0);
        System.out.println("向右轮转0个位置后: " + Arrays.toString(nums5));
        // 预期输出: [1]
    }
}
